<template>
    <div v-if="adc.useinfo.AId">
        <h1>房型添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <th align="right">房型名称</th>
                    <th><input type="text" v-model="queryinfo.HName"></th>
                </tr>
                <tr>
                    <th align="right">单价</th>
                    <th><input type="text" v-model="queryinfo.HPrice"></th>
                </tr>
                <tr>
                    <th align="right">室内面积</th>
                    <th><input type="text" v-model="queryinfo.HArea"></th>
                </tr>
                <tr>
                    <th align="right">最多入住人数</th>
                    <th><input type="text" v-model="queryinfo.HNumber"></th>
                </tr>
                <tr>
                    <th align="right">房型图片</th>
                    <th>
                        <input type="file" @change="imgs">
                        <img :src="queryinfo.HPhoto" width="100" height="100" v-if="queryinfo.HPhoto != ''">
                    </th>
                </tr>
                <tr>
                    <th align="right">房型描述</th>
                    <th>
                        <textarea cols="50" rows="5" v-model="queryinfo.HDesc"></textarea>
                    </th>
                </tr>
                <tr>
                    <th align="right">是否有窗</th>
                    <th>
                        <input type="radio" name="win" :value="1" v-model="queryinfo.IsWin">是
                        <input type="radio" name="win" :value="0" v-model="queryinfo.IsWin">否
                    </th>
                </tr>
                <tr>
                    <th align="right"></th>
                    <th><input type="button" value="增加" class="btn btn-primary" @click="ok"></th>
                </tr>
            </tbody>
        </table>
    </div>
    <div v-else>
        你还没有登录
        <input type="button" value="登录" class="btn btn-primary" @click="router.push('/adminlogin')">
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import moment from 'moment';
import { useRoute, useRouter } from 'vue-router';
import { useAdminStare } from '@/stores/admin';
const adc = useAdminStare();
const route = useRoute()
const router = useRouter()

let queryinfo: any = reactive({
    "HName": "",
    "HPrice": "",
    "HArea": "",
    "HNumber": "",
    "HPhoto": "",
    "HDesc": "",
    "IsWin": 1,
})


const imgs = (e: any) => {
    var obj = e.target.files[0]
    let f = new FormData()
    f.append('file', obj)
    axios({
        url: '/api/Photo/Upload',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == "不是图片") {
                alert("不是图片")
                return
            }
            if (res.data == "大于2M") {
                alert("大于2M")
                return
            }
            queryinfo.HPhoto = res.data
        })
}

const ok = () => {
    if (queryinfo.HName == "") {
        alert("房型名称不能为空")
        return
    }
    if (queryinfo.HPrice == "") {
        alert("单价不能为空")
        return
    }
    let reg = /^[0-9]+(\.[0-9]+)?$/
    if (reg.test(queryinfo.HPrice) == false) {
        alert("单价只能是正数")
        return
    }
    if (queryinfo.HPhoto == "") {
        alert("房型图片不能为空")
        return
    }
    axios({
        url: '/api/House/AddHouseType',
        method: 'post',
        data: queryinfo
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert("房型名称重复")
                return
            }
            if (res.data > 0) {
                alert("添加成功")
                router.push("/houselist")
            }
            else {
                alert("添加失败")
                return
            }
        })
}



</script>

<style scoped></style>